<?php
namespace app\api\controller;
use think\Db;
use think\Controller;
use think\Log;
use app\service\apixcxService;
use app\service\visitorService;
use app\service\staffService;
use app\service\wotuService;
use app\service\deviceService;
class Visitor extends Controller
{
	//查询访客的信息
	public function userquery(){
		$user_id=request()->post("user_id");
		if(strlen($user_id)!="16"){
			return json(["errCode"=>"1",'errMsg' => "用户信息获取失败"]);
		}
		$visitinfo=(new visitorService())->visitorUserid($user_id);
		if(!empty($visitinfo)){
			return json(["errCode"=>"0"]);
		}else{
			return json(["errCode"=>"2"]);
		}
	}

	//添加用户操作
	public function visitoradd()
	{
		$user_id=request()->post("user_id");
		$data=request()->post();
		$userinfo=(new staffService())->staffUserid($user_id);
		if(empty($userinfo)){
			return json(["errCode"=>"1",'errMsg' => '用户添加失败']);
		}
        $data["parent_user_id"]=$data["user_id"];
        $data["user_id"]=0;
		//查询该手机号是否添加
		$visitorinfo=(new visitorService())->visitorPhone($data["phone"]);
		if(!empty($visitorinfo)){
			return json(["errCode"=>"1",'errMsg' => '该用户已经添加']);
		}
		$send=sms_visitor($data["phone"],$data["name"],$userinfo["name"]);
        //Log::write($send,"123");
		if($send['Message'] != 'OK') {
			return json(["errCode"=>"1",'errMsg' => '用户添加失败']);
		}
		//查询
		$data["company_name"]=$userinfo["company_name"];
		$data["company_id"]=$userinfo["company_id"];
		$data["staff_id"]=$userinfo["id"];
		$data["staff_name"]=$userinfo["name"];
		$data["create_time"]=time();

		$res=(new visitorService())->visitoradd($data);
		if($res)
		{

			return json(["errCode"=>"0",'errMsg' => '用户添加成功']);
		}else
		{
			return json(["errCode"=>"1",'errMsg' => '用户添加失败,请重新添加']);
		}
	}
	
	//
	

	//
	public function piclist(){
		$user_id=request()->post("user_id");
		if(strlen($user_id)!="16"){
			return json(["errCode"=>"1",'errMsg' => "您还没有验证"]);
		}
		$visitorinfo=(new visitorService())->visitorUserid($user_id);
		if(empty($visitorinfo))
		{
			return json(["errCode"=>"1",'errMsg' => '您还没有验证']);
		}
        if($visitorinfo["personGuid"]=="0")
        {
            $type="3";  //访客       
            $re=(new wotuService())->reg($visitorinfo["name"],$visitorinfo["phone"],$type,"","");
            
            $result=json_decode($re,true);
            $guid=$result["data"]["guid"];
            Db::table("visitor")->where("id",$visitorinfo["id"])->update(["personGuid"=>$guid]);
            $visitorinfo["personGuid"]=$guid;
        }
        $data["userinfo"]=$visitorinfo;
        $res=(new wotuService())->tupian($visitorinfo["personGuid"]);
        $data["pic"]=$res["data"];
        $num=count($res["data"]);
        $data["num"]=$num;
        $data["uploadstatus"]=true;
        if($num=="1"){
            $data["uploadstatus"]=false;
        }
        return json(["errCode"=>"0",'datainfo' =>$data]);
	}


	//上传图片处理
    public function uploadAction()
    {
        $id=request()->post("id");
        $zim_id=request()->post("zim_id");
        $user_id=request()->post("user_id");
        $data["biz_id"]=$user_id;
        $data["zim_id"]=$zim_id;
        $api=new apixcxService();
       	Log::write(request()->post(),"data");
        $res=$api->faceimgquery(json_encode($data));
        if($res->code=="10000"){
            //查询成功
            $rest=$res->extern_info;
            $re=json_decode($rest,true);
            $img=$re["imgStr"];
            $image=$this->urlsafe_b64decode($img);
            $userinfo=Db::table("visitor")->where("id",$id)->find();
            //Log::write($userinfo,"userinfo");
            $guid=$userinfo["personGuid"];
            $result=(new wotuService())->uploadimg($image,$guid);
            //Log::write($result,"result");
            if($result["code"]=="GS_SUS600"){
                //查询该小区下的人脸设备并授权
                $locklist=(new deviceService())->visitordeviceList($user_id);
                $deviceKeys="";
                if(empty($locklist)){
                    return json(["errCode"=>"0",'msg' =>"图片上传成功"]); 
                }
                foreach ($locklist as $key => $value) {
                    $deviceKeys=$deviceKeys.$value["devicekey"].",";
                }
                $deviceKeys=rtrim($deviceKeys,",");
                (new wotuService())->accredit($guid, $deviceKeys);
                
                 return json(["errCode"=>"0",'msg' =>"图片上传成功"]);
            }else{
                return json(["errCode"=>"1",'errmsg' =>"图片上传失败"]);
            }
        }
    }
    public function delimage(){
        $guid=request()->post("guid");
        $id=request()->post("id");
        $userinfo=Db::table("visitor")->where("id",$id)->find();
        if(empty($userinfo))
        {
            return json(["errCode"=>"1",'errMsg' => '图片删除失败']);
        }
        if(empty($userinfo["personGuid"])){
            return json(["errCode"=>"1",'errMsg' => '图片删除失败']);
        }
        $personGuid=$userinfo["personGuid"];
        $result=(new wotuService())->delimage($personGuid,$guid);
        //图片删除成功
        if($result["code"]=="GS_SUS601"){
            return json(["errCode"=>"1",'msg' => '图片删除成功']);
            
        }else{
            return json(["errCode"=>"1",'errMsg' => '图片删除失败']);
        }
    }
    private function urlsafe_b64decode($string) {
       $data = str_replace(array('-','_'),array('+','/'),$string);
       $mod4 = strlen($data) % 4;
       if ($mod4) {
           $data .= substr('====', $mod4);
       }
       return $data;
       return base64_decode($data);
    }
	
}